package cn.operation.backend.service;

import cn.operation.backend.enums.DeleteStatusEnum;
import cn.operation.base.mapper.CWaybillMapper;
import cn.operation.base.mapper.ItfLatestCodePondMapper;
import cn.operation.base.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class WayBillServiceImpl implements WayBillService {
    private static final Logger LOGGER = LoggerFactory.getLogger(WayBillServiceImpl.class);
    @Autowired
    private CWaybillMapper cWaybillMapper;
    @Autowired
    private ItfLatestCodePondMapper itfLatestCodePondMapper;

    @Override
    public WaybillDO queryByWaybillCode(String waybillCode) throws Exception {
        if (waybillCode == null) {
            LOGGER.error("WayBillServiceImpl.queryByWaybillCode param waybillCode must not be null ");
            throw new IllegalArgumentException("运单编号为空");
        }
        WaybillDO waybillDO = null;
        try {
            waybillDO = cWaybillMapper.queryByWaybillCode(waybillCode);
        } catch (Exception e) {
            LOGGER.error("WayBillServiceImpl.queryByWaybillCode param query waybillDetail data error. ");
            throw new IllegalArgumentException("查询运单明细数据异常:"+ e);
        }
        try {
            ItfLatestCodePondExample example = new ItfLatestCodePondExample();
            example.createCriteria().andCodeEqualTo(waybillCode).andStatusEqualTo(DeleteStatusEnum.NORMAL.getValue());
            List<ItfLatestCodePond> list = itfLatestCodePondMapper.selectByExample(example);
            if (list.size()>0) {
                ItfLatestCodePond codePond = new ItfLatestCodePond();
                codePond.setId(list.get(0).getId());
                codePond.setStatus(DeleteStatusEnum.DELETRE.getValue());
                itfLatestCodePondMapper.updateByPrimaryKeySelective(codePond);
            }
        } catch (Exception e) {
            LOGGER.error("WayBillServiceImpl.queryByWaybillCode param update ItfLatestCodePond data error: ");
            throw new IllegalArgumentException("修改业务单据号码池单据状态为删除状态异常:"+ e);
        }
        return waybillDO;
    }

    @Override
    public Integer updateShipTo(String waybillCode, Date shipToDate) throws Exception {
        CWaybillExample example = new CWaybillExample();
        example.createCriteria().andWaybillCodeEqualTo(waybillCode);
        List<CWaybill> list = cWaybillMapper.selectByExample(example);
        if (list.size()==0){
            LOGGER.error("WayBillServiceImpl.updateShipTo param query data is null ");
            throw new IllegalArgumentException("未查询到该运单号运单");
        }
        CWaybill cWaybill = new CWaybill();
        cWaybill.setId(list.get(0).getId());
        cWaybill.setArriveDate(shipToDate);
        //运单状态，90：已完成
        cWaybill.setStatus(90);
        return cWaybillMapper.updateByPrimaryKeySelective(cWaybill);
    }
}
